package com.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;

public class MyDatabase extends SQLiteOpenHelper{

	public static final String TABLE_ID = "Numbers";
	public static final String COLUMN_ID = "_id";
	public static final String COLUMN_NUM = "_numbers";
	private static final String DATABASE_NAME = "ids.db";
	private static final int DATABASE_VERSION = 1;
	private static final String DATABASE_CREATE = "CREATE TABLE " + TABLE_ID + "("
            + COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_NUM + " TEXT"+ ")";
	
	public MyDatabase(Context context) {
	    super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase database) {
	    database.execSQL(DATABASE_CREATE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	    Log.w(MyDatabase.class.getName(),
	        "Upgrading database from version " + oldVersion + " to "
	            + newVersion + ", which will destroy all old data");
	    db.execSQL("DROP TABLE IF EXISTS " + TABLE_ID);
	    onCreate(db);
	  }

	/**
	 * All CRUD(Create, Read, Update, Delete) Operations
	 */
	
	// Adding new contact
    void addData(Data data) {
        SQLiteDatabase db = this.getWritableDatabase();
 
        ContentValues values = new ContentValues();
        values.put(COLUMN_NUM, data.getNumber()); // Data Name
 
        // Inserting Row
        db.insert(TABLE_ID, null, values);
        db.close(); // Closing database connection
    }
 
    // Getting single contact
    Data getData(int id) {
        SQLiteDatabase db = this.getReadableDatabase();
 
        Cursor cursor = db.query(TABLE_ID, new String[] { COLUMN_ID, COLUMN_NUM }, COLUMN_ID + "=?",
                new String[] { String.valueOf(id) }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();
 
        Data data = new Data(Integer.parseInt(cursor.getString(0)), Long.parseLong(cursor.getString(1)));
        // return contact
        return data;
    }
 
    // Getting All Data
    public List<Data> getAllDatas() {
        List<Data> contactList = new ArrayList<Data>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_ID;
 
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
 
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
            	Data data = new Data(Integer.parseInt(cursor.getString(0)), Long.parseLong(cursor.getString(1)));
                // Adding contact to list
                contactList.add(data);
            } while (cursor.moveToNext());
        }
 
        // return contact list
        return contactList;
    }
 
    // Updating single contact
    public long updateData(Data data) {
        SQLiteDatabase db = this.getWritableDatabase();
 
        ContentValues values = new ContentValues();
        values.put(COLUMN_NUM, data.getNumber());
 
        // updating row
        return db.update(TABLE_ID, values, COLUMN_ID + " = ?",
                new String[] { String.valueOf(data.getID()) });
    }
 
    // Deleting single contact
    public void deleteData(Data data) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_ID, COLUMN_ID + " = ?",
                new String[] { String.valueOf(data.getID()) });
        db.close();
    }
 
    // Getting contacts Count
    public long getDatasCount() {
        String countQuery = "SELECT  * FROM " + TABLE_ID;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();
 
        // return count
        return cursor.getCount();
    }
    
    public void deleteAllData(){
    	String selectQuery = "SELECT  * FROM " + TABLE_ID;
    	 
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
 
        // looping through all rows and deleting from list
        if (cursor.getCount()>0) {
            do {
            	db.execSQL("delete * from "+TABLE_ID);
            } while (cursor.moveToNext());
        }
    }
}
